<!--
 * @Author: wcx 1055127249@qq.com
 * @Date: 2024-12-26 10:37:55
 * @LastEditors: wcx 1055127249@qq.com
 * @LastEditTime: 2025-01-17 15:25:43
 * @FilePath: \my-project\src\components\commonBizTable\components\dialogForm.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog v-bind="$attrs" :title="title" :visible.sync="visible" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <baseForm ref="baseForm" :key="key" validator v-model="form" v-bind="formConfig" hideSubmit />
    <div slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import baseForm from './baseForm'
export default {
  name: 'formDialog',
  components: {
    baseForm
  },
  data() {
    return {
      title: '',
      key: new Date().getTime(),
      form: {},
      formConfig: {},
      visible: false
    }
  },
  methods: {
    async init(table, detail = false) {
      const { fields = [], formConfig = {} } = table || {}
      const formFields = fields.filter(i => i.form).map(i => ({ ...i, type: i.formType }))
      this.formConfig = { labelWidth: '120px', ...formConfig, detail }
      this.key = new Date().getTime()
      await this.$nextTick()
      this.$refs.baseForm.setFileds(formFields)
    },
    add(table) {
      this.title = '新增'
      this.form = JSON.parse(JSON.stringify({}))
      this.init(table)
      this.visible = true
    },
    edit(data, table) {
      this.title = '编辑'
      this.form = JSON.parse(JSON.stringify(data))
      this.init(table)
      this.visible = true
    },
    detail(data, table, title = '详情') {
      this.title = title
      this.form = JSON.parse(JSON.stringify(data))
      this.init(table, true)
      this.visible = true
    },
    close() {
      this.visible = false
    },
    async submit() {
      try {
        await this.$refs.baseForm.$refs.form.validate()
        this.$emit('submit', this.form)
        console.log('this.form', this.form)
        this.visible = false
      } catch (error) {
        console.log('error', error)
      }
    }
  }
}
</script>